using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace GameEditor.Common.Controls
{
    public interface IImageHolder
    {
        Image Image { get;}
    }
    public class ComboBoxExItem : NamedObject, IImageHolder
    {

        private Image image;
        private int level = 0;

        public int Level
        {
            get { return level; }
            set { level = value; }
        }
        

        public Image Image
        {
            get
            {
                if (this.Delegation != null && Delegation is IImageHolder)
                    return (Delegation as IImageHolder).Image;
                return image; 
            }
            set 
            {
                image = value; 
            }
        }
        public ComboBoxExItem(Image image, string text, object delegation)
            : base(text, delegation)
        {
            this.image = image;
        }
        public ComboBoxExItem(Image image, int level, string text, object delegation)
            : base(text, delegation)
        {
            this.level = level;
            this.image = image;
        }
        public ComboBoxExItem()
        {

        }
        public ComboBoxExItem(Image image, string text)
            : base(text, null)
        {
            this.image = image;
        }

    }


    public class ComboBoxEx : ComboBox
    {
        private ImageAttributes imageAttributes;
        public ComboBoxEx()
        {
            this.DropDownStyle = ComboBoxStyle.DropDownList;
            this.DrawMode = DrawMode.OwnerDrawFixed;

            imageAttributes = new ImageAttributes();
            imageAttributes.SetColorKey(Color.Fuchsia, Color.Fuchsia);
            
        }
        protected override void Dispose(bool disposing)
        {
            imageAttributes.Dispose();
            base.Dispose(disposing);
        }
        private Color imageTransparentColor = Color.Fuchsia;

        public Color ImageTransparentColor
        {
            get { return imageTransparentColor; }
            set 
            {
                imageTransparentColor = value;
                imageAttributes.SetColorKey(value, value);
            }
        }


        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            Rectangle rect = e.Bounds;
            int iconX = rect.X + 2;
            int iconY = rect.Y;
            e.DrawBackground();
            if (e.Index >= 0)
            {
                object item = Items[e.Index];
                string s = item.ToString();

                


                IImageHolder holder = item as IImageHolder;
                ComboBoxExItem itemEx = item as ComboBoxExItem;
                int indent = 0;
                int indentWidth = 8;
                if (itemEx != null)
                    indent = itemEx.Level;

                if (holder != null)
                {
                    iconX += indent * indentWidth;
                    rect.X += indentWidth * indent;

                    Image image = holder.Image;
                    if (image != null)
                    {
                        e.Graphics.DrawImage(image, new Rectangle(iconX, iconY, 16, 16), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);
                    }
                }

                rect.X += 20;



                Brush brush = Brushes.Black;
                if ((e.State & DrawItemState.Focus) != 0)
                    brush = Brushes.White;

                e.Graphics.DrawString(s, e.Font, brush, rect);



            }
            e.DrawFocusRectangle();

        }

    }
}
